package com.yunxiao.cloud.web.config;

import com.yunxiao.cloud.common.property.RedisKeyPrefixProperties;
import com.yunxiao.cloud.common.serializer.PrefixStringKeySerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

/**
 * @description: RedisConfig
 * @author: lintiangang
 * @date: 2020/8/24 9:49
 * @version: V1.0
 **/
@Configuration
public class RedisConfig {

    @Autowired
    private RedisKeyPrefixProperties redisKeyPrefixProperties;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 支持key前缀设置的key Serializer
        redisTemplate.setKeySerializer(new PrefixStringKeySerializer(redisKeyPrefixProperties));
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}
